import requests

# from Wallpaper.models import Wallpaper, Category
from Wallpaper.models import LiveCategory, LiveWallpaper


class BirdWallpaperSpider:
    def __init__(self, category):
        self.category = category
        self.url = f"http://digbird.shanhutech.cn/intf/getCategoryRank?category={category}" \
                   "&type=2&pageno=1&count=1000&appver=2.1.6&channel=oppo&mid=2B3031036AB9F2B5BA77AD0D2A548A4A2CF57966" \
                   "&model=OPPO%20R11&deviceStatus=old&globalPaperSet=0"
        # self.url = "http://digbird.shanhutech.cn/intf/getRecommendList?type=2&pageno=1&count=1000&appver=2.1.6" \
        #            "&channel=oppo&mid=2B3031036AB9F2B5BA77AD0D2A548A4A2CF57966&model=OPPO%20R11&deviceStatus=old" \
        #            "&globalPaperSet=1"

        self.live_url = f"http://digbird.shanhutech.cn/intf/getCategoryList?category={category}&type=1&pageno=1" \
                        "&count=10000&appver=2.1.6&channel=oppo&mid=2B3031036AB9F2B5BA77AD0D2A548A4A2CF57966&model=OPPO%20R11" \
                        "&deviceStatus=old&globalPaperSet=1"
        self.headers = {
            "UserAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0",
        }

    def main(self):
        req = requests.get(self.url).json()
        for data in req['data']['list']:
            print(data)
            # try:
            #     category = Category.objects.get(name=data['category'])
            # except:
            #     category = Category(name=data['category'])
            #     category.save()
            # try:
            #     wallpaper = Wallpaper(id=data['id'], title=data['title'], category=category, tag=data['tag'],
            #                           resolution=data['resolution'], file_size=data['file_size'], url=data['url'],
            #                           create_time=data['create_time'])
            #     wallpaper.save()
            # except:
            #     print("已存在")
        print(f"{self.category}下共有{len(req['data']['list'])}张壁纸")

    def get_live_wallpaper(self):
        req = requests.get(self.live_url).json()
        for data in req['data']['list']:
            print(data)
            try:
                category = LiveCategory.objects.get(name=data['first_category'])
            except:
                category = LiveCategory(name=data['first_category'])
                category.save()
            try:
                wallpaper = LiveWallpaper(id=data['id'], title=data['title'], category=category, tag=data['tags'],
                                          preview=data['image'], resolution=data['video_size'], url=data['url_preview'],
                                          file_size=data['file_size_preview'],
                                          create_time=data['create_time'])
                wallpaper.save()
            except Exception as e:
                print(e)
                print("已存在")
        print(f"{self.category}下共有{len(req['data']['list'])}张壁纸")


if __name__ == "__main__":
    BirdWallpaperSpider(category="动漫").get_live_wallpaper()
